
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import core


# 筛选中文、年份
def Choose_year(web):
    count = 0
    while True:
        try:
            time.sleep(10)
            # 点击中文
            web.find_element(By.XPATH, '/html/body/div[3]/div[1]/div/div/div/a[1]').click()
            time.sleep(8)
            web.find_element(By.XPATH, '//*[@id="divGroup"]/dl[3]/dt/i[1]').click()
            time.sleep(8)
            web.find_element(By.XPATH, '//*[@id="divGroup"]/dl[3]/dt/i[2]').click()
            time.sleep(8)
            web.find_element(By.XPATH, '//*[@id="txtStartYear"]').send_keys(2020)
            time.sleep(3)
            web.find_element(By.XPATH, '//*[@id="txtEndYear"]').send_keys(2022)
            time.sleep(3)
            web.find_element(By.XPATH, '//*[@id="btnFilterYear"]').click()
            break
        except:
            web.refresh()
            # 重新输入
            web.find_element(By.XPATH, '//*[@id="txt_search"]').clear()
            time.sleep(2)
            web.find_element(By.XPATH, '//*[@id="txt_search"]').send_keys(core.search, Keys.ENTER)
            count += 1
            # 如果刷新了15次还不行 就睡
            if count > 15:
                time.sleep(120)
            if count > 21:
                break
